<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  <style>
    #todo{
      max-width: 600px;
      border: 1px solid #999;
      border-radius: 10px;
      margin: 20px auto;
      padding: 20px;
    }
    .todo-content{
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div id="app">
    <div id="todo">
      <div class="todo-header row">
         <div class="col-md-10">
          <input type="text" v-model.trim="inputTxt" class="form-control">
         </div>
         <div class="col-md-2">
          <button @click="addTodo" class="btn btn-primary">增加</button>
         </div>
      </div>
      <div class="todo-content">
        <ul class="list-group">
          <li class="list-group-item text-center" v-if="todos.length === 0">
            暂无数据
          </li>
          <li 
            class="list-group-item clearfix"
            v-for="(todo, index) in todos"
          >
            <span>
              {{ todo.content }}
            </span>
            <div class="pull-right">
              <button class="btn btn-xs btn-danger" @click="delTodo(index)">删除</button>
            </div>
          </li>
         
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
   
    const vm = new Vue({
      el: '#app',
      data: {
        // 绑定输入框值
        inputTxt: '',
        // 待办事项列表
        todos: [
          {
            content: '明天放假去漂流',
            done: false
          },
          {
            content: '后天放假去游泳',
            done: false
          },
          {
            content: '8月20号去看病',
            done: false
          }
        ]
      },
      methods: {
        // 新增待办事项
        addTodo(){
          // 判断 输入框value为空  不 新增
          if (this.inputTxt === '') {
            return false;
          }
          this.todos.push({
            content: this.inputTxt,
            done: false
          })
          // 表单value清空
          this.inputTxt = ''
        },
        // 删除待办事项
        delTodo(index){
          if(confirm('确认删除吗?')){
            this.todos.splice(index, 1);
          }
        }
      }
    })
  </script>
</body>
</html>